﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;

/// <summary>
/// SqlHelper 的摘要描述
/// </summary>
/// 
namespace com.pro2e.console.generic.util
{
    public class FileUploadHelper
    {
        public FileUploadHelper()
        {

        }

        /// <summary>
        /// 檢查資料夾..沒有的話就建立
        /// </summary>
        public static void CheckDir(string strFilePath)
        {
            if (!Directory.Exists(strFilePath))
            {
                Directory.CreateDirectory(strFilePath);
            }
        }

        /// <summary>
        /// 檢查檔案是否已經存在
        /// </summary>
        public static bool CheckFile(string strFilePath)
        {
            if (File.Exists(strFilePath))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 檢查圖片寬度是否超過
        /// </summary>
        public static Boolean CheckPicPX(FileUpload myFileUpload, int allowWidth)
        {
            Stream PicFile = myFileUpload.PostedFile.InputStream;
            System.Drawing.Image Pic = System.Drawing.Image.FromStream(PicFile);
            if (Pic.Width <= allowWidth)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 檢查副檔名
        /// </summary>
        public static Boolean CheckFuFileName(FileUpload myFileUpload, string[] allowExtensions)
        {
            bool allow = false;

            string uploadFuFileName = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
            //string[] allowExtensions ={ ".jpg", ".gif" };

            for (int i = 0; i < allowExtensions.Length; i++)
            {
                if (uploadFuFileName == allowExtensions[i])
                {
                    allow = true;
                }
            }

            return allow;
        }

        /// <summary>
        /// 刪除檔案
        /// </summary>
        /// <param name="filePath"></param>
        public static void DeleteUploadFile(string filePath)
        {
            File.Delete(filePath);
        }

    }
}